package com.hmkj.core.service.userAuth.impl;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.common.utils.DateUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.mapper.user.UserMapper;
import com.hmkj.core.mapper.userAuth.UserAuthMapper;
import com.hmkj.core.mapper.userIdentify.UserIdentifyMapper;
import com.hmkj.core.po.user.User;
import com.hmkj.core.po.userAuth.UserAuth;
import com.hmkj.core.po.userIdentify.UserIdentify;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.userAuth.UserAuthService;
import com.hundsun.t2sdk.common.util.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户实名认证记录业务相关Service接口实现<br>
 *
 * @ClassName: UserAuthServiceImpl
 * @author lizhijie
 * @date   2018-07-04 04:34:26
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class UserAuthServiceImpl implements UserAuthService {

	@Resource
    private UserAuthMapper userAuthMapper;
	@Resource
    private UserMapper userMapper;
	@Resource
    private UserIdentifyMapper userIdentifyMapper;

    @Override
    public GenericMapper<UserAuth,Integer> _getMapper() {
        return userAuthMapper;
    }

    @Override
    public UserAuth selectByUID(Integer userId) {
        return userAuthMapper.selectByUID(userId);
    }

    @Override
    public void doVerify(UserAuth userAuth) throws Exception {
        UserAuth auth = userAuthMapper.selectByPrimaryKey(userAuth.getId());
        if (auth == null){
            throw new BussinessException("审核记录不存在");
        }
        if (auth.getAuthStatus() != UserAuth.AUTHSTATUS.NORMAL.code){
            throw new BussinessException("审核状态不正确");
        }
        if (StringUtils.isEmpty(userAuth.getRemark())){
            throw new BussinessException("请添加备注");
        }
        auth.setAuthStatus(userAuth.getAuthStatus());
        auth.setRemark(userAuth.getRemark());
        auth.setCertificateTime(DateUtils.getNow());
        userAuthMapper.updateByPrimaryKeySelective(auth);
        if (userAuth.getAuthStatus() == UserAuth.AUTHSTATUS.CERTIFICATED.code) {
            //用户认证表也要修改
            UserIdentify userIdentify = userIdentifyMapper.selectOne(new UserIdentify(a->{
                a.setUserId(auth.getUserId());
            }));

            userIdentifyMapper.updateByPrimaryKeySelective(new UserIdentify(t->{
                t.setId(userIdentify.getId());
                t.setRealnameStatus(UserIdentify.REALNAMESTATUS.T3.code);
                t.setRealnameVerifyTime(DateUtils.getNow());
                t.setRealnameVerifyRemark(userAuth.getRemark());
            }));

            //用户表更新真实姓名和身份证号码
            userMapper.updateByPrimaryKeySelective(new User(t->{
                t.setId(auth.getUserId());
                t.setRealName(auth.getRealname());
                t.setIdCard(auth.getIdcard());
            }));
        }
    }

    @Override
    public void addRealNameAuth(User user,String realName,String idCard) {
        UserAuth userAuth = userAuthMapper.selectByUID(user.getId());
        if (null != userAuth && userAuth.getAuthStatus() == UserAuth.AUTHSTATUS.CERTIFICATED.code) {
            throw new BussinessException("您的实名认证审核已通过，请勿重复认证");
        }
        //校验身份证号码是否被使用过
        UserAuth userAuth1 = userAuthMapper.selectOne(new UserAuth(t->{
            t.setIdcard(idCard);
        }));
        if(null != userAuth1){
            throw new BussinessException("该身份证号码已被使用，请更换其他身份证号码");
        }
        if (null == userAuth) {
            userAuthMapper.insert(new UserAuth(t -> {
                t.setUserId(user.getId());
                t.setRealname(realName);
                t.setIdcard(idCard);
//                t.setPositiveIdcardPic(cardFront);
//                t.setOppositeIdcardPic(cardOpposite);
                t.setType(UserAuth.TYPE.MANUAL_AUTH.code);
                t.setAuthStatus(UserAuth.AUTHSTATUS.NORMAL.code);
            }));
        } else if (userAuth.getAuthStatus() != UserAuth.AUTHSTATUS.CERTIFICATED.code) {
            userAuthMapper.updateByPrimaryKeySelective(new UserAuth(t -> {
                t.setId(userAuth.getId());
                t.setUserId(user.getId());
                t.setRealname(realName);
                t.setIdcard(idCard);
//                t.setPositiveIdcardPic(cardFront);
//                t.setOppositeIdcardPic(cardOpposite);
                t.setType(UserAuth.TYPE.MANUAL_AUTH.code);
                t.setAuthStatus(UserAuth.AUTHSTATUS.NORMAL.code);
            }));
        }
        //更新用户认证记录
        userIdentifyMapper.updateByPrimaryKeySelective(new UserIdentify(t->{
            t.setId(userIdentifyMapper.selectByUID(user.getId()).getId());
            t.setRealnameStatus(UserIdentify.REALNAMESTATUS.T2.code);
        }));

    }

}
